<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
        <div class="header-logo">
            <img src="@/assets/logo.png" alt="">
            <span>电商后台管理系统</span>
        </div>
        <el-button type='info' @click="logout">退出</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse ? '70px':'250px'">
            <!-- 侧边栏折叠 -->
            <div class="toggle-button" @click="toggleCollapse">|||</div>
            <el-menu
            :collapse='isCollapse'
            :collapse-transition = 'false'
            unique-opened
            background-color="#333744"
            text-color="#fff"
            active-text-color="#ffd04b"
            router
            :default-active='activePath'>
                <!-- 一级菜单 -->
                <el-submenu :index="item.id.toString()" v-for="item in menusList" :key="item.id">
                    <template slot="title">
                    <i :class="iconsObj[item.id]"></i>
                    <span>{{item.authName}}</span>
                    </template>
                    <el-menu-item
                    :index=" '/home/' +sonItem.path "
                    v-for="sonItem in item.children"
                    :key="sonItem.id"
                    @click="saveNavState('/home/' +sonItem.path)">
                        <template slot="title">
                            <i class="el-icon-menu"></i>
                            <span>{{sonItem.authName}}</span>
                        </template>
                    </el-menu-item>
                </el-submenu>
            </el-menu>
        </el-aside>
        <!-- 左侧内容 -->
        <el-main>
            <router-view></router-view>
        </el-main>
    </el-container>
</el-container>
</template>

<script>
/* eslint-disable no-unused-expressions */

import { instance } from '../utils/http'
export default {
  async created () {
    const { data: res } = await instance.get('/menus')
    if (res.meta.status !== 200) return this.$message.console.error(res.meta.msg)
    this.menusList = res.data
    this.activePath = sessionStorage.getItem('activePath')
  },
  data () {
    return {
      menusList: [],
      iconsObj: {
        125: 'el-icon-s-custom',
        103: 'el-icon-s-platform',
        101: 'el-icon-s-shop',
        102: 'el-icon-s-claim',
        145: 'el-icon-s-data'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  methods: {
    logout () {
      sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    saveNavState (data) {
      sessionStorage.setItem('activePath', data)
      this.activePath = data
    }
  }
}
</script>

<style scoped lang="less">
.home-container{
    height: 100%;
}
.el-header{
    background-color: #373D41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;
    line-height: 25px;
    img{
        width: 50px;
        height: 50px;
    }
    .header-logo{
        display: flex;
        align-items: center;
    }
    span{
        margin-left: 15px;
    }
}
.el-aside{
    background-color: #333744;
    width: 240px;
    .el-menu{
        border: none;
    }
}
.el-main{
    background-color: #EAEDF1;
}
.toggle-button{
    background-color: #4A5064;
    display: flex;
    justify-content: center;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    letter-spacing: 0.2em;
    cursor: pointer;
}
</style>
